package wnhz.com.business.groupshop.fragment;

import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import wnhz.com.business.R;
import wnhz.com.business.base.BaseFragment;
import wnhz.com.business.base.BaseRVAdapter;
import wnhz.com.business.base.BaseViewHolder;
import wnhz.com.business.uitls.CommomDialog;

/**
 * Created by Administrator on 2017/9/15 0015.
 * 商品中心，全部商品碎片
 */

public class AllShangpinFragment extends BaseFragment {

    @BindView(R.id.recycler_data)
    RecyclerView recyclerData;
    Unbinder unbinder;
    private View view;

    private List<String> item = new ArrayList<>();
    private final int TOST = 1;

    public static AllShangpinFragment newInstance() {
        return new AllShangpinFragment();
    }


    private Handler mhandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                //跳弹窗
                case TOST:
                    new CommomDialog(getContext(), R.style.dialog, "您确定要将该商品上下架吗？", new CommomDialog.OnCloseListener() {
                        @Override
                        public void onClick(Dialog dialog, boolean confirm) {
                            if (confirm == true) {
                                dialog.dismiss();
                            } else {
                                dialog.dismiss();
                            }

                        }
                    }).show();
                    break;
            }

        }
    };

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_allshangpin, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initview();
        setRecyclerData();
    }

    private void initview() {
        item.clear();
        for (int i = 0; i < 4; i++) {
            item.add("ha" + i);
        }
        recyclerData.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
    }

    private void setRecyclerData() {
        recyclerData.setAdapter(new BaseRVAdapter(getContext(), item) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_allshangpin;
            }

            @Override
            public void onBind(BaseViewHolder holder, int position) {
                holder.getTextView(R.id.tv_down).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mhandler.sendEmptyMessage(TOST);
                    }
                });
            }

        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
